﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.DAL.DAOs;

namespace LibrarySystem.Controllers.LibraryControllers
{
    public class EmployeeController : Controller
    {
        Library_Employees libEmp = new Library_Employees();

        [HttpPost]
        public JsonResult EmployeeOptions(string empUserName = null)
        {
            try
            {
                List<EmployeeModel> empModel = libEmp.getEmpToComboBox(empUserName);
                var empOptions = empModel.Select(s => new { Value = s.ID, DisplayText = s.UserName });

                return Json(new { Result = "OK", Options = empOptions });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult GetEmployeeList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null,
            string name = null)
        {
            try
            {
                int count = libEmp.GetDataCount();
                List<EmployeeModel> empList = libEmp.GetDataList(jtStartIndex, jtPageSize, jtSorting, name);
                return Json(new { Result = "OK", Records = empList, TotalRecordCount = count });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult AddEmployee(EmployeeModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                var add = libEmp.AddNewData(model);
                return Json(new { Result = "OK", Record = add });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult UpdateEmployee(EmployeeModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { Result = "Error", Message = "The data is invalid. Please try again!" });
                }
                libEmp.UpdateData(model);
                return Json(new { Result = "OK" });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "Error", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult DeleteEmployee(int Id)
        {
            return null;
        }
    }
}
